package com.hava.unit;

import java.sql.*;

/**
 * Created by yanfa on 2016/10/1.
 */
public class StudentDAO {

    public final static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    public final static String DB_URL = "jdbc:mysql://192.168.1.200/test";
    public final static String USER = "root";
    public final static String PASS = "dVHJtG0T:pf*";

    public static void getStudent(String name) throws ClassNotFoundException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select name,score from student where name ='" + name +"'");
            System.out.println("[SQL]:" + "select name,score from student where name ='" + name +"'");
            while (rs.next()) {
                System.out.println(rs.getString("name") + ":" + rs.getInt("score"));
            }
        } catch (SQLException e) {
            // ignore
            System.out.println("[SQLException]:" + e.toString());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                    // ignore
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // ignore
                }
            }
        }
    }


    public static void getStudentSafe(String name) throws ClassNotFoundException {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            String sql = "SELECT name,score FROM student WHERE name = ?";

            System.out.println("SQL:" + sql);

            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,name);
            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                System.out.println(rs.getString("name") + ":" + rs.getInt("score"));
            }
        } catch (SQLException e) {
            // ignore
            System.out.println("[SQLException]:" + e.toString());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    // ignore
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (Exception e) {
                    // ignore
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // ignore
                }
            }
        }
    }
}
